#pragma once
#include <cmath>
#include <limits>

#ifndef	TAU
#define TAU 6.283185307179586476925286766559
#endif

#ifndef PI
#define PI 3.1415926535897932384626433832795
#endif

#ifndef EULERCONST
#define EULERCONST 2.71828182845904523536028747135266249775724709369995
#endif

#ifndef INFINITY
#define INFINITY std::numeric_limits<float>::infinity()
#endif

inline float invSqrt(float x)
{
	float xhalf = 0.5f*x;
	union
	{
		float x;
		int i;
	} u;

	u.x = x;
	u.i = 0x5f3759df - (u.i >> 1);
	u.x = u.x * (1.5f - xhalf * u.x * u.x); /* This line can be repeated arbitrarily many times to increase accuracy */
	return u.x;
}


inline void Clamp(float &arg, float min, float max)
{
	if (arg < min)
	{
		arg = min;
	}

	if (arg > max)
	{
		arg = max;
	}
}

inline unsigned int FloatToUnsignedInt(float value, float min, float max)
{
	float u = (value-min) / (max-min);
	unsigned int iValue = (unsigned int)(u * (float)((unsigned int)-1));
	return iValue;
}

inline float UnsignedIntToFloat(unsigned int value, float min, float max)
{
	float u = (float)value / (float)((unsigned int)-1);
	float fValue = min + u * (max-min);
	return fValue;
}

inline int RandomRange(int min, int max)
{
	return min + (rand() % (int)(max - min + 1));
}